//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>

Aura_Bool AuraTest_Test3()
{
    Aura_Address virAddr  = 0;
    Aura_Address baseAddr = 0;
    Aura_MemorySize size  = 0x1000;
    Aura_Bool bIsValid = AURA_FALSE;
    Aura_MemorySize querySize;

    Aura_ECode ec = AURA_NOERROR;

    ec = AuraVirtualMemory_Map(0,
                              size,
                              Aura_MemoryMap_Upgrow,
                              Aura_MemoryProtection_Read,
                              &virAddr);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_Map> failed. (ec = 0x%x)\n", ec);
        goto ErrorExit;
    }

    Aura_printf(" allocated virtual memory address: 0x%x.\n", virAddr);
    ec = AuraVirtualMemory_CheckProtection(virAddr,
                                        Aura_MemoryProtection_Read,
                                        &bIsValid);
    if (AURA_FAILED(ec) || !bIsValid) {
        Aura_printf(" <AuraVirtualMemory_CheckProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_SetProtection(virAddr, size, Aura_MemoryProtection_Write);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_SetProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_CheckProtection(virAddr,
                                        Aura_MemoryProtection_Write,
                                        &bIsValid);
    if (AURA_FAILED(ec) || !bIsValid) {
        Aura_printf(" <AuraVirtualMemory_CheckProtection> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_Commit(virAddr);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_Commit> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    ec = AuraVirtualMemory_Query(virAddr, &baseAddr, &querySize);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_Query> failed. (ec = 0x%x).\n",ec);
    }
    else {
        if (querySize != size) {
            Aura_printf(" the size is not matched (queryed:0x%x, size:0x%x).\n",
                    querySize, size);
            goto ErrorExit;
        }
    }

    ec = AuraVirtualMemory_Unmap(virAddr, size);
    if (AURA_FAILED(ec)) {
        Aura_printf(" <AuraVirtualMemory_Unmap> failed. (ec = 0x%x).\n",ec);
        goto ErrorExit;
    }

    return AURA_TRUE;

ErrorExit:
    AuraVirtualMemory_Unmap(virAddr, size);
    return AURA_FALSE;
}
